package logic

import (
	"B/models/mysql"
	"context"

	"B/internal/svc"
	"B/shenzhousrv"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsListLogic {
	return &GoodsListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsListLogic) GoodsList(in *shenzhousrv.GoodsListRequest) (*shenzhousrv.GoodsListResponse, error) {
	var goodsList []mysql.Goods
	if in.Status == "" {
		mysql.DB.Debug().Find(&goodsList)
	} else {
		mysql.DB.Debug().Where("status = ?", in.Status).Find(&goodsList)
	}

	var lists []*shenzhousrv.GoodsList
	for _, v := range goodsList {
		lists = append(lists, &shenzhousrv.GoodsList{
			GoodsId: int64(v.ID),
			Title:   v.Title,
			Cover:   v.Cover,
			Price:   float32(v.Price),
			Stock:   int64(v.Stock),
			Status:  v.Status,
		})
	}
	return &shenzhousrv.GoodsListResponse{
		List: lists,
	}, nil
}
